﻿using FlyingEye.Come.Repositories;
using FlyingEye.DeviceMaintenance;
using FlyingEye.DeviceMaintenance.Repositories;
using FlyingEye.Exceptions;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;

namespace FlyingEye.DeviceMaintenanceServices
{
    public class DeviceMaintenanceService : ApplicationService
    {
        private readonly IDeviceMaintenanceRepository _deviceMaintenanceRepository;

        private readonly IComeGroupNotificationConfigurationRepository _comeGroupNotificationRepository;

        private readonly IComeUserNotificationConfigurationRepository _comeUserNotificationRepository;

        public DeviceMaintenanceService(
            IDeviceMaintenanceRepository deviceMaintenanceRepository,
            IComeGroupNotificationConfigurationRepository comeGroupNotificationRepository,
            IComeUserNotificationConfigurationRepository comeUserNotificationRepository)
        {
            _deviceMaintenanceRepository = deviceMaintenanceRepository;
            _comeGroupNotificationRepository = comeGroupNotificationRepository;
            _comeUserNotificationRepository = comeUserNotificationRepository;
        }

        public async Task<DeviceMaintenanceResponse> GetAsync(Guid id)
        {
            try
            {
                await _deviceMaintenanceRepository.GetAsync(id);
                var model = await _deviceMaintenanceRepository.GetAsync(id);

                return ObjectMapper.Map<DeviceMaintenanceModel, DeviceMaintenanceResponse>(model);
            }
            catch (EntityNotFoundException)
            {
                throw new ResourceNotFoundException($"项目 id = {id} 不存在！");
            }
        }

        /// <summary>
        /// 获取所有项目
        /// </summary>
        public async Task<List<DeviceMaintenanceResponse>> GetListAsync()
        {
            var modes = await _deviceMaintenanceRepository.GetListAsync();

            return ObjectMapper.Map<List<DeviceMaintenanceModel>, List<DeviceMaintenanceResponse>>(modes);
        }

        /// <summary>
        /// 添加一个设备保养项目
        /// </summary>
        public async Task<DeviceMaintenanceResponse> InsertAsync(CreateDeviceMaintenanceRequest deviceMaintenance)
        {
            if (await _deviceMaintenanceRepository.AnyAsync(item => item.DeviceName == deviceMaintenance.DeviceName))
            {
                throw new BusinessOperationException($"设备名称 DeviceName = {deviceMaintenance.DeviceName} 在设备主机 DeviceHostId = {deviceMaintenance.DeviceHostId} 已经被使用！");
            }

            var model = ObjectMapper.Map<CreateDeviceMaintenanceRequest, DeviceMaintenanceModel>(deviceMaintenance);

            model = await _deviceMaintenanceRepository.InsertAsync(model);

            return ObjectMapper.Map<DeviceMaintenanceModel, DeviceMaintenanceResponse>(model);
        }

        /// <summary>
        /// 设置 Come 用户通知配置
        /// </summary>
        private async Task VerifyComeUserNotification(Guid comeUserNotificationConfigurationId)
        {
            var user = await _comeUserNotificationRepository.FindAsync(comeUserNotificationConfigurationId);

            if (!await _comeUserNotificationRepository.AnyAsync(item => item.Id == comeUserNotificationConfigurationId))
            {
                throw new ResourceNotFoundException($"COME 用户配置 id = {comeUserNotificationConfigurationId} 不存在！");
            }
        }

        /// <summary>
        /// 设置项目关联的组
        /// </summary>
        public async Task VerifyComeGroupNotificationAsync(Guid comeGroupNotificationConfigurationId)
        {
            if (!await _comeGroupNotificationRepository.AnyAsync(item => item.Id == comeGroupNotificationConfigurationId))
            {
                throw new ResourceNotFoundException($"COME 群组配置 id = {comeGroupNotificationConfigurationId} 不存在！");
            }
        }

        /// <summary>
        /// 删除一个项目
        /// </summary>
        public async Task DeleteAsync(Guid id)
        {
            if (!await _deviceMaintenanceRepository.AnyAsync(item => item.Id == id))
            {
                throw new ResourceNotFoundException($"项目 id = {id} 不存在！");
            }

            await _deviceMaintenanceRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 更新一个项目
        /// </summary>
        public async Task<DeviceMaintenanceResponse> UpdateAsync(UpdateDeviceMaintenanceRequest deviceMaintenance)
        {
            if (deviceMaintenance.ComeGroupNotificationId.HasValue && deviceMaintenance.ComeGroupNotificationId != Guid.Empty)
            {
                await VerifyComeGroupNotificationAsync(deviceMaintenance.ComeGroupNotificationId.Value);
            }

            if (deviceMaintenance.ComeUserNotificationId.HasValue && deviceMaintenance.ComeUserNotificationId != Guid.Empty)
            {
                await VerifyComeUserNotification(deviceMaintenance.ComeUserNotificationId.Value);
            }

            if (await _deviceMaintenanceRepository.AnyAsync(item => item.DeviceName == deviceMaintenance.DeviceName))
            {
                throw new BusinessOperationException($"设备名称 DeviceName = {deviceMaintenance.DeviceName} 在设备主机 DeviceHostId = {deviceMaintenance.DeviceHostId} 已经被使用！");
            }

            var model = await _deviceMaintenanceRepository.FindAsync(deviceMaintenance.Id);

            if (model == null)
            {
                throw new ResourceNotFoundException($"项目 id = {deviceMaintenance.Id} 不存在！");
            }

            ObjectMapper.Map(deviceMaintenance, model);

            model = await _deviceMaintenanceRepository.UpdateAsync(model);

            return ObjectMapper.Map<DeviceMaintenanceModel, DeviceMaintenanceResponse>(model);
        }
    }
}
